//
//  Slot.swift
//  Hyphenate-Demo-Swift
//
//  Created by JennyShi on 2018/4/19.
//  Copyright © 2018年 杜洁鹏. All rights reserved.
//

import UIKit
import os.log

class Slot: NSObject, NSCoding {
    
    struct PropertyKey {
        static let startTime = "startTime"
        static let endTime = "endTime"
    }
    
    //MARK: Properties
    
    var estartTime: String
    var eendTime: String
    
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("slotsArr")
    
    //MARK: Initialization
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(estartTime, forKey: PropertyKey.startTime)
        aCoder.encode(eendTime, forKey: PropertyKey.endTime)
    }
    
    required convenience init?(coder aDecoder: NSCoder) {
        // The title is required. If we cannot decode a name string, the initializer should fail.
        guard var startTime = aDecoder.decodeObject(forKey: PropertyKey.startTime) as? String else {
            os_log("Unable to decode the startTime for a Slot", log: OSLog.default, type: .debug)
            return nil
            
        }
        
        startTime = (aDecoder.decodeObject(forKey: PropertyKey.startTime) as? String)!
        let endTime = aDecoder.decodeObject(forKey: PropertyKey.endTime) as? String
        
        self.init(startTime : startTime, endTime : endTime!)
        
    }
    
    init?(startTime : String, endTime : String) {
        
        // Initialization should fail if there is no name or if the rating is negative.
        if startTime.isEmpty || endTime.isEmpty  {
            return nil
        }
        
        self.estartTime = startTime
        self.eendTime = endTime
        
    }
    
}

